package by.vkhalko.asamo.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.vkhalko.asamo.dataaccess.PersonDao;
import by.vkhalko.asamo.datamodel.Company;
import by.vkhalko.asamo.datamodel.Person;
import by.vkhalko.asamo.services.PersonService;

@Service
public class PersonServiceImpl implements PersonService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(PersonServiceImpl.class);

	@Inject
	private PersonDao dao;

	@PostConstruct
	private void init() {
		LOGGER.info("Instance of PersonService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public void saveOrUpdate(Person person) {
		if (person.getId() == null) {
			LOGGER.debug("Save new: {}", person);
			dao.insert(person);
		} else {
			LOGGER.debug("Update: {}", person);
			dao.update(person);
		}
	}

	@Override
	public void delete(Person person) {
		LOGGER.debug("Remove: {}", person);
		dao.delete(person.getId());
	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all persons");
		dao.deleteAll();
	}

	@Override
	public Person get(Long id) {
		Person entity = dao.getById(id);
		return entity;
	}

	@Override
	public List<Person> getAllByCompany(Company company) {
		return dao.getAllByCompany(company);
	}

}
